package com.miguo.framework.view.pulltorefresh.internal;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import com.miguo.framework.R;
import com.miguo.framework.view.pulltorefresh.PullToRefreshBase;

/**
 * File Names: HeaderAnimLoadingLayout
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-08-27
 * Time: 11:32
 */
public class HeaderAnimLoadingLayout extends LoadingLayout {

    private Animation mUpAnima;

    private Animation mDownAnima;

    private AnimationDrawable mHeaderAnimeDrawable;

    private boolean mAnimaState = false;

    public HeaderAnimLoadingLayout(Context context, final PullToRefreshBase.Mode mode, final PullToRefreshBase.Orientation scrollDirection, TypedArray attrs) {
        super(context, mode, scrollDirection, attrs);

        mUpAnima = new TranslateAnimation(0.0F, 0.0F, Utils.dip2px(context, 48.0F), 0.0F);
        mUpAnima.setDuration(500L);
        mDownAnima = new TranslateAnimation(0.0F, 0.0F, 0.0F, Utils.dip2px(context, 48.0F));
        mDownAnima.setDuration(500L);
    }

    @Override
    protected void onLoadingDrawableSet(Drawable imageDrawable) {
    }

    @Override
    protected void onPullImpl(float scaleOfLayout) {
        // NO-OP
    }

    // 下拉刷新
    @Override
    protected void pullToRefreshImpl() {
        switch (mMode) {
            case PULL_FROM_END:
                break;
            case PULL_FROM_START:
            default:
                mHeaderAnimeImage.setVisibility(View.VISIBLE);
                mHeaderAnimeImage.setImageResource(R.drawable.ptr_refresh_pull);
                if (mUpAnima != null && mAnimaState == false) {
                    mAnimaState = true;
                    mHeaderAnimeImage.startAnimation(mUpAnima);
                }
                break;
        }
    }

    // 正在刷新
    @Override
    protected void refreshingImpl() {
        switch (mMode) {
            case PULL_FROM_END:
                break;
            case PULL_FROM_START:
            default:
                mHeaderAnimeImage.setImageResource(R.drawable.ptr_refresh_refreshing);
                mHeaderAnimeDrawable = (AnimationDrawable) mHeaderAnimeImage.getDrawable();
                mHeaderAnimeDrawable.start();
                break;
        }

    }

    // 松开刷新
    @Override
    protected void releaseToRefreshImpl() {
        switch (mMode) {
            case PULL_FROM_END:
                break;
            case PULL_FROM_START:
            default:
                //mHeaderAnimeDrawable = (AnimationDrawable) mHeaderAnimeImage.getDrawable();
                //mHeaderAnimeDrawable.stop();
                mHeaderAnimeImage.setImageResource(R.drawable.ptr_refresh_release);
                break;
        }
    }

    // 重新设置
    @Override
    protected void resetImpl() {
        switch (mMode) {
            case PULL_FROM_END:
                mHeaderProgress.setVisibility(View.GONE);
                mHeaderImage.setVisibility(View.GONE);
                mHeaderAnimeImage.setVisibility(View.GONE);
                break;
            case PULL_FROM_START:
            default:
                mHeaderProgress.setVisibility(View.GONE);
                mHeaderImage.setVisibility(View.GONE);
                if (mDownAnima != null && mAnimaState) {
                    mAnimaState = false;
                    mHeaderAnimeImage.startAnimation(mDownAnima);
                }
                mHeaderAnimeImage.setImageResource(R.drawable.ptr_refresh_pull);
                mHeaderAnimeImage.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected int getDefaultDrawableResId() {
        return R.drawable.default_ptr_flip;
    }

}